package com.sky.controller.admin;


import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品管理")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品")
    public Result save(@RequestBody DishDTO dishDTO) {

        log.info("新增菜品：{}", dishDTO);
        dishService.save(dishDTO);

        //如果新增菜品更新缓存
        String key = "dish_" + dishDTO.getCategoryId();
        clearCache(key);

        return Result.success();
    }
    /**
     * 菜品管理-分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO) {
        log.info("分页查询：{}", dishPageQueryDTO);
        PageResult pageResult =dishService.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }
    /**
     * 菜品管理-批量删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result delete(@RequestParam List<Long> ids) {
        log.info("批量删除：{}", ids);
        dishService.deleteByIds(ids);

        //删除菜品更新缓存
        clearCache("dish_*");

        return Result.success();
    }

    /**
     * 菜品管理-查询菜品ById
     **/
    @GetMapping("/{id}")
    public Result<DishVO> getById(@PathVariable Long id) {
        log.info("查询菜品ById：{}", id);
        DishVO dishVO = dishService.getById(id);
        return Result.success(dishVO);
    }

    /**
     * 菜品管理-修改菜品ById
     **/
    @PutMapping
    public Result update(@RequestBody DishDTO dishDTO) {

        log.info("修改菜品ById：{}", dishDTO);
        dishService.update(dishDTO);
        //修改菜品更新缓存

        clearCache("dish_*");
        return Result.success();
    }

    @PostMapping("/status/{status}")
    public Result startOrStop(@PathVariable Integer status, Long id) {

        log.info("启用或禁用菜品：{}", id);
        dishService.startOrStop(status, id);
        //启用或禁用菜品更新缓存
        clearCache("dish_*");

        return Result.success();
    }

    @GetMapping("/list")
    public Result<List<DishVO>> list(String categoryId) {
        log.info("查询菜品列表：{}", categoryId);
        List<DishVO> list = dishService.list(categoryId);
        return Result.success(list);

    }

    private void clearCache(String pattern) {
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
}
